
let div: HTMLDivElement | null = null;

let hadrequest = false;
window.onload = () =>
{


    let tag = Plugin_Init((json) =>
    {
        if (json["cmd"] == "access.request")
        {
            let succ = json["succ"] as boolean;
            hadrequest = succ;
        }
        if (json["cmd"] == "folder.list")
        {
            if (div != null)
            {
                let i = div.children.length - 1;
                for (var j = i; j >= 0; j--)
                {
                    div.removeChild(div.children[j]);
                }
            }
            var list = json["list"];
            for (var l in list)
            {
                let item = list[l];
                let spanf = document.createElement("span");
                if (item["type"] == "file")
                {
                    document.body.appendChild(spanf);
                    spanf.textContent = "FILE: name=" + item["name"] + " : SIZE:" + item["size"];
                    div?.appendChild(spanf);
                }
                else
                {
                    document.body.appendChild(spanf);
                    spanf.textContent = "PATH: name=" + item["name"];
                    div?.appendChild(spanf);

                }
                div?.appendChild(document.createElement("br"));
            }
        }

        console.log("recv from plugin:" + JSON.stringify(json));
    });


    console.warn("helloworld.");

    var span = document.createElement("span");
    document.body.appendChild(span);
    span.textContent = "检测插件=" + tag;
    document.body.appendChild(document.createElement("br"));

    var btn = document.createElement("button");
    btn.innerText = "列表 D:\\";
    document.body.appendChild(btn);
    var btn2 = document.createElement("button");
    btn2.innerText = "创建 D:\\123.txt";
    document.body.appendChild(btn2);

    document.body.appendChild(document.createElement("hr"));

    div = document.createElement("div");
    document.body.appendChild(div);

    btn.onclick = () =>
    {
        if (tag)
        {
            if (!hadrequest)
                Plugin_Send("access.request", 0, { "path": "d:\\" });
            Plugin_Send("folder.list", 0, { "path": "." });
        }
    };
    btn2.onclick = () =>
    {
        if (tag)
        {
            if (!hadrequest)
                Plugin_Send("access.request", 0, { "path": "d:\\" });
            Plugin_Send("file.write", 0, { "path": "123.txt", "format": "string", "data": "这是从网页写过来的" });
        }
    }
    if (tag)
    {
        Plugin_Send("ping", 0);

    }


};
function IsPluginInited(): boolean
{
    let b = PluginBrdige != null;
    return b;
}
var PluginBrdige: HTMLElement | null = null;

function Plugin_Init(onrecv: (data: any) => void): boolean
{
    //插件的plugin 可以安装一个dom组件

    //我们可以通过这个dom组件去确定插件是否存在
    PluginBrdige = document.getElementById('--webkit-call-bridge');
    let tag = PluginBrdige != null;
    if (tag)
    {
        let Plugin_OnRecv = onrecv;
        PluginBrdige?.addEventListener("--content2user", (ev: Event) =>
        {
            var event = ev as CustomEvent;
            Plugin_OnRecv(event.detail);
        })

    }
    return tag;
}
function Plugin_Send(cmd: string, msgid: number, param: any = null): void
{
    var msg: any = { "native": "localfolder", "cmd": cmd, "msgid": msgid };
    if (param != null)
    {
        for (var key in param)
        {
            msg[key] = param[key];
        }
    }
    if (PluginBrdige != null)
    {
        var sendE: CustomEvent = document.createEvent("CustomEvent");
        sendE.initCustomEvent("--user2content", true, true, msg);
        PluginBrdige.dispatchEvent(sendE);
    }
}